/*============================================================================*/
/*                   CONTINENTAL AUTOMOTIVE SYSTEMS                           */
/*============================================================================*/
/*                        OBJECT SPECIFICATION                                */
/*============================================================================*
* name:            HALKnob.c
* version:         1.0
* created_by:      Continental
* date_created:    Ago 11 2011
*=============================================================================*/
/* DESCRIPTION : Source file for Abstraction layer for KNOB                   */
/*============================================================================*/
/* FUNCTION COMMENT : Describes the functions for Abstraction Layer for KNOB  */
/*                                                                            */
/*============================================================================*/
/*                               OBJECT HISTORY                               */
/*============================================================================*/
/*  REVISION |   DATE      |                               |      AUTHOR      */
/*----------------------------------------------------------------------------*/
/*  1.0      | 11/ago/2011 |                               | Continental      */
/* Initial version for the Smart Car Race 2010 contest using Freescale        */
/* Bolero micro.                                                              */
/*============================================================================*/



#include "HALLigth.h"

uint16_t	auto_flag_off =1;
static uint16_t	flag=1;
uint16_t	auto_flag_off2 =1;
static uint16_t	flag2=1;

void LigthsOff(void)
{
	if (flag==TRUE)
	{
		auto_flag_off =0;
		Fade_Ligths();
		flag=FALSE;
	}
	else if(flag2==TRUE)
	{
		auto_flag_off2=0;
		Fade_Ligths2();
		flag2=FALSE;
	}
	else
	{
		IO_PinWrite(pin_PA0,0);
		IO_PinWrite(pin_PA2,0);
		IO_PinWrite(pin_PA4,0);
		IO_PinWrite(pin_PA6,0);
		IO_PinWrite(pin_PA8,0);	
	}
}

void LigthsOff_LED(void)
{
	IO_PinWrite(pin_PE4,0); 
	IO_PinWrite(pin_PE5,1);
	IO_PinWrite(pin_PE6,1);
	IO_PinWrite(pin_PE7,1);
	
	/*7 Segment */
	
	IO_PinWrite(pin_PA1,0); 
	IO_PinWrite(pin_PA3,0);
	IO_PinWrite(pin_PA5,0);
	IO_PinWrite(pin_PA7,0);
	IO_PinWrite(pin_PA9,0);
	IO_PinWrite(pin_PA11,0);
	IO_PinWrite(pin_PA13,1);

}

void Parklamps(void)
{
	IO_PinWrite(pin_PA0,1);
	IO_PinWrite(pin_PA2,0);
	IO_PinWrite(pin_PA4,0);
	IO_PinWrite(pin_PA6,0);
	IO_PinWrite(pin_PA8,1);
	flag=1;
	flag2=0;
}

void Parklamps_LED(void)
{
	IO_PinWrite(pin_PE4,1); 
	IO_PinWrite(pin_PE5,0);
	IO_PinWrite(pin_PE6,1);
	IO_PinWrite(pin_PE7,1);

	/*7 Segment */
	
	IO_PinWrite(pin_PA1,0); 
	IO_PinWrite(pin_PA3,0);
	IO_PinWrite(pin_PA5,1);
	IO_PinWrite(pin_PA7,1);
	IO_PinWrite(pin_PA9,0);
	IO_PinWrite(pin_PA11,0);
	IO_PinWrite(pin_PA13,0);
}

void Headlamps(void)
{
	IO_PinWrite(pin_PA0,1);
	IO_PinWrite(pin_PA2,1);
	IO_PinWrite(pin_PA4,1);
	IO_PinWrite(pin_PA6,1);
	IO_PinWrite(pin_PA8,1);
	flag=0;
	flag2=1;
}

void Headlamps_LED(void)
{
	IO_PinWrite(pin_PE4,1); 
	IO_PinWrite(pin_PE5,1);
	IO_PinWrite(pin_PE6,0);
	IO_PinWrite(pin_PE7,1);
	
	/*7 Segment */
	
	IO_PinWrite(pin_PA1,1); 
	IO_PinWrite(pin_PA3,0);
	IO_PinWrite(pin_PA5,0);
	IO_PinWrite(pin_PA7,1);
	IO_PinWrite(pin_PA9,0);
	IO_PinWrite(pin_PA11,0);
	IO_PinWrite(pin_PA13,0);

}

void Auto_LED(void)
{
	IO_PinWrite(pin_PE4,1); 
	IO_PinWrite(pin_PE5,1);
	IO_PinWrite(pin_PE6,1);
	IO_PinWrite(pin_PE7,0);
	
		/*7 Segment */
	
	IO_PinWrite(pin_PA1,0); 
	IO_PinWrite(pin_PA3,0);
	IO_PinWrite(pin_PA5,0);
	IO_PinWrite(pin_PA7,1);
	IO_PinWrite(pin_PA9,0);
	IO_PinWrite(pin_PA11,0);
	IO_PinWrite(pin_PA13,0);
}

void Ligths_Init(void)
{
	InitPWM2();
	InitPWM3();
}
void Fade_Ligths(void)
{
uint16_t	i;
	if (auto_flag_off==0)
	{
		IO_SetPinFctn(pin_PB15,PA_AM1);
		for(i=1023; i>0; i --)
		{
		SetDutyCycle3(i);
		delay_ms(2);
		}
		auto_flag_off=1;
		IO_SetPinFctn(pin_PB15,PA_GPIO);
	}
	
}

void Fade_Ligths2(void)
{
uint16_t	i;
	if (auto_flag_off2==0)
	{
		IO_SetPinFctn(pin_PB15,PA_AM1);
		IO_SetPinFctn(pin_PB14,PA_AM1);
		for(i=1023; i>0; i --)
		{
		SetDutyCycle2(i);
		SetDutyCycle3(i);
		delay_ms(2);
		}
		auto_flag_off2=1;
		IO_SetPinFctn(pin_PB15,PA_GPIO);
		IO_SetPinFctn(pin_PB14,PA_GPIO);
	}
	
}

void delay_ms(T_UWORD var_ms)
{
uint16_t i,j;	
	for(i=0; i< var_ms ; i++)
	{
		for(j=0; j<6000; j++){}
	}
}
